//////////////////////////////////////////ok
#include"stdafx.h"

#include "bochs.h"



void  IA32_CPU::writeEFlags(Bit32u flags, Bit32u changeMask)
{
 
  Bit32u supportMask, newEFlags;
  supportMask = 0x00037fd5;
  supportMask |= ((1<<21) | (1<<18)); // ID/AC
  changeMask &= supportMask;
  newEFlags = ( eflags.val32 & ~changeMask) |(flags & changeMask);
   setEFlags( newEFlags );
   lf_flags_status = 0; // OSZAPC flags are known.

  if(newEFlags & (1 << 8)) 
  {
     async_event = 1; // TF = 1
  }
  else
	  debug_trap&=0xFFFFBFFF; // fix a bug for int 1
}

void  IA32_CPU::write_flags(Bit16u flags, bx_bool change_IOPL, bx_bool change_IF)
{
  Bit32u changeMask = 0x0dd5;
  changeMask |= (1<<14); // NT is modified as requested.

  if (change_IOPL)
    changeMask |= (3<<12); // IOPL is modified as requested.

  if (change_IF)
    changeMask |= (1<<9);

  writeEFlags(Bit32u(flags), changeMask);
}


void IA32_CPU::write_eflags(Bit32u eflags_raw, bx_bool change_IOPL,bx_bool change_IF, bx_bool change_VM, bx_bool change_RF)
{

  Bit32u changeMask = 0x4dd5;

  changeMask |= ((1<<21) | (1<<18)); // ID/AC
  if (change_IOPL)
    changeMask |= (3<<12);

  if (change_IF)
    changeMask |= (1<<9);

  if (change_VM)
    changeMask |= (1<<17);

  if (change_RF)
    changeMask |= (1<<16);

  writeEFlags(eflags_raw, changeMask);
}

Bit32u IA32_CPU::force_flags(void)
{
  if ( lf_flags_status) 
  {
    (void) get_CF();
    (void) get_PF();
    (void) get_AF();
    (void) get_ZF();
    (void) get_SF();
    (void) get_OF();
  }
  return  eflags.val32;
}

Bit16u IA32_CPU::read_flags(void)
{
   return(Bit16u)  force_flags();
}

Bit32u IA32_CPU::read_eflags(void)
{
  return force_flags();
}
